package com.office.automate.dataaccess.impl.administration;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.office.automate.dataaccess.def.administration.DivisionDao;
import com.office.automate.dataaccess.framework.DataAccessSessionFactory;
import com.office.automate.exception.administration.DivisionExceptionKeys;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.model.administration.Division;

public class DivisionDaoImplementer implements DivisionDao {

	private static SessionFactory sessionFactory = null;
	private Session session = null;

	static {
		sessionFactory = DataAccessSessionFactory.getSessionFactory();
	}

	public DivisionDaoImplementer() {
		session = sessionFactory.openSession();		
	}
	public int getAvailableDivisionDetailRecordCount()
			throws FrontierVisibleException {
		int recordCount = 0;
		try {
			 String SQL_QUERY ="select count(*) from Division division";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 recordCount = ((Long) queryResult.list().get(0)).intValue();
		} catch(Exception e) {
			throw new FrontierVisibleException(DivisionExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		}
		return recordCount;
	}

	@SuppressWarnings("unchecked")
	public List<Division> getAvailableDivisionsPage(int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {
		List<Division> aDivisionList = null;
		try {
			 String SQL_QUERY ="from Division division order by division.divisionId asc";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 queryResult.setFirstResult(aStartingIndex);
			 queryResult.setMaxResults(aNumberOfRecords);
			 aDivisionList = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(DivisionExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return aDivisionList;
	}

	public Division persistDivisionDetails(Division aDivision) throws FrontierVisibleException {
		try {
			Transaction transaction = session.beginTransaction();
			session.save(aDivision);
			transaction.commit();
		} catch(Exception e) {
			throw new FrontierVisibleException(DivisionExceptionKeys.FAIL_TO_PERSIST_DIVISION, e);
		} finally {
			session.flush();
			session.close();
		}
		return aDivision;
	}

	public void deleteDivision(Division aDivision) throws FrontierVisibleException {
		try {
			session.beginTransaction();   
			session.delete(aDivision);  
			session.getTransaction().commit();  
		} catch(Exception e) {
			throw new FrontierVisibleException(DivisionExceptionKeys.ERROR_OCCORRED_DURING_DELETION, e);
		} finally {
			session.flush();
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Division> getAvailableDivisions() throws FrontierVisibleException {

		List<Division> availableDivisions = null;
		try {
			 String SQL_QUERY ="from Division division";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 availableDivisions = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(DivisionExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return availableDivisions;
	}

	public String getDivisionName(int divisionId) throws FrontierVisibleException {
		String divisionName = "";
		try {
			String SQL_QUERY ="select division.name from Division division where division.divisionId = :divisionId";
			Query query = session.createQuery(SQL_QUERY);
			query.setParameter("divisionId", divisionId);
			divisionName = (String) query.list().get(0);
		} catch(Exception e) {
			throw new FrontierVisibleException(DivisionExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}

		return divisionName;
	}
}